package com.naiterui.ehp.bs.emr.service.impl;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.constants.InquirerRelationEnum;
import com.naiterui.ehp.bs.emr.entity.mongo.BaseMedicalRecordConsultRoom;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecord;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecordScreen;
import com.naiterui.ehp.bs.emr.repository.mongo.MedicalRecordScreenRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 病历共有方法
 *
 * @author wangzehao
 * @date 2018/11/20 8:10 PM
 * @since 1.0.0
 */
public abstract class AbstractMedicalRecordServiceImpl {
    @Autowired
    protected MedicalRecordScreenRepository medicalRecordScreenRepository;

    /**
     * 获取主诉
     *
     * @return
     */
    String getMainComplaint(BaseMedicalRecordConsultRoom medicalRecordConsultRoom) {
        Integer type = Optional.ofNullable(medicalRecordConsultRoom.getType()).orElse(BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_OLD);
        if (type == BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_OLD) {
            List<String> mainComplaintList = Lists.newArrayList();
            String consultTheme = medicalRecordConsultRoom.getConsultTheme();
            mainComplaintList.add(StringUtils.isNotEmpty(consultTheme) ? "咨询主题：" + consultTheme : null);

            String mainComplaint = medicalRecordConsultRoom.getMainComplaint();
            mainComplaintList.add(StringUtils.isNotEmpty(mainComplaint) ? "病情主诉：" + mainComplaint : null);

            String sickenDate = medicalRecordConsultRoom.getSickenDate();
            mainComplaintList.add(StringUtils.isNotEmpty(sickenDate) ? "患病时长：" + sickenDate : null);

            String useDrug = medicalRecordConsultRoom.getUseDrug();
            mainComplaintList.add(StringUtils.isNotEmpty(useDrug) ? "用药情况：" + useDrug : null);
            return Joiner.on(StringUtils.LF).skipNulls().join(mainComplaintList);
        }

        if (type == BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_NEW) {
            List<String> mainComplaintList = Lists.newArrayList();
            String consultTheme = medicalRecordConsultRoom.getConsultTheme();
            mainComplaintList.add(StringUtils.isNotEmpty(consultTheme) ? "疾病主题：" + consultTheme : null);

            Integer firstDiagnosis = medicalRecordConsultRoom.getFirstDiagnosis();
            if (firstDiagnosis != null) {
                mainComplaintList.add(firstDiagnosis == BaseMedicalRecordConsultRoom.FIRSTDIAGNOSIS_TYPE_YES ? "初诊" : "复诊");
            }

            String diseaseManagement = medicalRecordConsultRoom.getDiseaseManagement();
            mainComplaintList.add(StringUtils.isNotEmpty(diseaseManagement) ? "疾病处理情况：" + diseaseManagement : null);

            String mainComplaint = medicalRecordConsultRoom.getMainComplaint();
            mainComplaintList.add(StringUtils.isNotEmpty(mainComplaint) ? "主诉/现病史：" + mainComplaint : null);

            String symptomsOfTime = medicalRecordConsultRoom.getSymptomsOfTime();
            mainComplaintList.add(StringUtils.isNotEmpty(symptomsOfTime) ? "症状时长：" + symptomsOfTime : null);

            String otherUnwell = medicalRecordConsultRoom.getOtherUnwell();
            mainComplaintList.add(StringUtils.isNotEmpty(otherUnwell) ? "其他不适：" + otherUnwell : null);

            return Joiner.on(StringUtils.LF).skipNulls().join(mainComplaintList);
        }
        return null;
    }

    /**
     * 获取既往史
     *
     * @return
     */
    String getPastHistory(BaseMedicalRecordConsultRoom medicalRecordConsultRoom) {
        Integer type = Optional.ofNullable(medicalRecordConsultRoom.getType()).orElse(BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_OLD);
        String pastHistory = medicalRecordConsultRoom.getPastHistory();
        String medicAllergys = medicalRecordConsultRoom.getMedicAllergys();
        if (type == BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_OLD) {
            List<String> pastHistoryStr = Lists.newArrayList();
            pastHistoryStr.add(StringUtils.isNotEmpty(pastHistory) ? "既往病史：" + pastHistory : null);
            pastHistoryStr.add(StringUtils.isNotEmpty(medicAllergys) ? "药物过敏史：" + medicAllergys : null);
            return Joiner.on(StringUtils.LF).skipNulls().join(pastHistoryStr);
        }

        if (type == BaseMedicalRecordConsultRoom.CONSULT_ROOM_TYPE_NEW) {
            List<String> pastHistoryStr = Lists.newArrayList();
            pastHistoryStr.add(StringUtils.isNotEmpty(pastHistory) ? "既往慢性病史：" + pastHistory : null);
            pastHistoryStr.add(StringUtils.isNotEmpty(medicAllergys) ? "药品食物过敏史：" + medicAllergys : null);
            return Joiner.on(StringUtils.LF).skipNulls().join(pastHistoryStr);
        }
        return null;

    }

    /**
     * 保存病历筛选条件
     *
     * @param medicalRecord
     *
     * @return
     */
    void saveMedicalRecordScreen(MedicalRecord medicalRecord) {
        // 保存病历记录关系筛选条件
        Long doctorId = medicalRecord.getDoctorId();
        Long patientId = medicalRecord.getPatientId();
        Integer relation = medicalRecord.getRelation();
        boolean screenFalg = medicalRecordScreenRepository.existsByDoctorIdAndPatientIdAndRelation(doctorId, patientId, relation);
        if (screenFalg) {
            return;
        }
        MedicalRecordScreen medicalRecordScreen = MedicalRecordScreen.newBuilder()
                                                                     .doctorId(doctorId)
                                                                     .patientId(patientId)
                                                                     .relation(relation)
                                                                     .relationName(InquirerRelationEnum.getNameByValue(relation))
                                                                     .createAt(new Date())
                                                                     .changedAt(new Date())
                                                                     .build();
        medicalRecordScreenRepository.save(medicalRecordScreen);
    }
}
